<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Contains examples of various Kohana library examples. You can access these
 * samples in your own installation of Kohana by going to ROOT_URL/examples.
 * This controller should NOT be used in production. It is for demonstration
 * purposes only!
 *
 * $Id: examples.php 4298 2009-04-30 17:06:05Z kiall $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Admin_Controller extends Controller {

        function __construct(){
          parent::__construct();
          $this->session = Session::instance();
          $usr = $this->session->get('usr');
          $pwd = $this->session->get('pwd');
          $p_usr = trim($this->input->post('usr'));
          $p_pwd = $this->_crypt_me(trim($this->input->post('pwd')));
          if(request::method() == 'post' and $p_pwd <> '' and $p_usr <> ''){
             $usr = trim($this->input->post('usr'));
             $pwd = $this->_crypt_me(trim($this->input->post('pwd')));
          }
          if($usr <> '' and $pwd <> '' ){
            $db = Database::Instance();
            $rows = $db->from("admin")
              ->where(array('usr_admin'=>$usr,'pwd_admin'=>$pwd))->get();
            $ct = 0;
            foreach($rows as $row){
              $ct++;
            }
            if($ct > 0){
              $this->session->set('usr', $usr);
              $this->session->set('pwd', $pwd);
             if(Router::$method <> 'panel'){
               url::redirect("/admin/panel");
             }
            }
          }else{
             if(Router::$method <> 'index'){
               url::redirect("/admin/index");
             }
          }

          if($this->uri->segment('mod') == 'logout'){
            $this->session->destroy();
          }
        
        }


        private function _crypt_me($str){
          return fpp::cryptme($str);
        }

        function index(){
          $tmp = new View('admingen/login');
          echo $tmp->render();
        }
         
         
        function panel(){
          $admin = new KfppAdmin();
          $admin->SetModelDefault('admin');
          $admin->SetFastText(array("admin"=>'Administracion','logout'=>'Salir') );
          $admin->SetModule("Usuarios",'admin');
          $admin->SetModule("Salir",'logout');
          $tmp = new View('admingen/admin');
          $stradmin = (string) $admin;
          $tmp->set('content',$stradmin);
          echo $tmp->render();
        }

}
